from datetime import datetime
from app import db

# 超市申请表模型
class SupermarketApplication(db.Model):
    __tablename__ = 'tb_supermarket_application'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), nullable=False, comment='超市名称')
    address = db.Column(db.String(255), nullable=False, comment='详细地址')
    business_hours = db.Column(db.String(100), comment='营业时间')
    latitude = db.Column(db.Numeric(10, 6), comment='纬度')
    longitude = db.Column(db.Numeric(10, 6), comment='经度')
    contact_name = db.Column(db.String(50), nullable=False, comment='联系人姓名')
    contact_phone = db.Column(db.String(20), nullable=False, comment='联系电话')
    remark = db.Column(db.Text, comment='备注说明')
    status = db.Column(db.String(20), default='pending', comment='状态(pending-待审核,approved-已批准,rejected-已拒绝)')
    apply_time = db.Column(db.DateTime, default=datetime.now, comment='申请时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    
    def __repr__(self):
        return f'<SupermarketApplication {self.name}>'
    
    def to_dict(self):
        """转换为字典，用于API响应"""
        return {
            'id': self.id,
            'name': self.name,
            'address': self.address,
            'business_hours': self.business_hours,
            'latitude': float(self.latitude) if self.latitude else None,
            'longitude': float(self.longitude) if self.longitude else None,
            'contact_name': self.contact_name,
            'contact_phone': self.contact_phone,
            'remark': self.remark,
            'status': self.status,
            'apply_time': self.apply_time.strftime('%Y-%m-%d %H:%M:%S') if self.apply_time else None,
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None
        } 